import Taro from '@tarojs/taro';
import u from 'updeep'
import storage from '../util/storage'
import request from '../util/request'

export default {
  namespace: 'login',
  state: {
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
      }
    }
  },
  effects: {
    loginMini: function* ({call}) {
      return Taro.login()
        .then(response => {

          return Taro.request({
            url: SERVERURL + "miniLogin?code=" + response.code,
            method: 'POST',
          })
            .then(res => {
              if (res.statusCode === 200) {
                storage.setStorage(res.header["Set-Cookie"])
              } else if (res.status === 500) {
                Taro.showToast({
                  title: '发生错误，请重试！',
                  icon: 'none'
                })
              }
              Taro.navigateBack({
                delta: 1
              })
            })
        })
        .catch(err => {
          Taro.showToast({
            title: '发生错误，请重试!',
            icon: 'none'
          })
          Taro.navigateBack({
            delta: 1
          })
        })

    },

  }
};
